from flask import Blueprint, render_template,request,redirect,url_for
from services.BookService import BookService
from model.Book import Book
routes_bp = Blueprint("routes", __name__)


@routes_bp.route("/book/")
def index():
    # return 'Hello, book!'
    bookService = BookService()
    books = bookService.list()
    return render_template("books.html", books=books)


@routes_bp.route("/book/add")
def add():
    return render_template("add_book.html")


@routes_bp.route("/book/add", methods=["POST"])
def add_post():
    title = request.form.get("title")
    author = request.form.get("author")
    price = request.form.get("price")
    book=Book(title,author,price)
    bookService = BookService()
    books = bookService.add(book)
    return redirect(url_for('routes.index'))
